﻿package com.tomvian.base
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageQuality;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Thomas Vian
	 */
	public class Base extends Sprite
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public static var base:Base;
		public static var stage:Stage;
		public static var root:DisplayObject;
		
		protected var _customURLs:Dictionary;
		
		protected var _rightClickMenu:ContextMenu;
		
		protected var _highQuality:ContextMenuItem;
		protected var _mediumQuality:ContextMenuItem;
		protected var _lowQuality:ContextMenuItem;
		protected var _siteLink:ContextMenuItem;
		
		protected const _highQualityText:String = 	"Quality: High";
		protected const _mediumQualityText:String = "Quality: Medium";
		protected const _lowQualityText:String = 	"Quality: Low";
		
		//--------------------------------------------------------------------------
		//	
		// Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//	
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Base()
		{
			if (stage) 	init();
			else 		addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			game = this;
			gameStage = stage;
			gameRoot = root;
			
			_customURLs = new Dictionary;
			
			createRightClickMenu();
		}
		
		protected function clearPreloader():void
		{
			if (parent && parent is Preloader)
			{
				Preloader(parent).clearLoader();
			}
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Quality Methods
		//
		//--------------------------------------------------------------------------
		
		public function qualityToHigh(e:Event = null):void
		{
			stage.quality = StageQuality.HIGH;
			
			_highQuality.caption = 		_highQualityText + " <<<";
			_mediumQuality.caption = 	_mediumQualityText;
			_lowQuality.caption = 		_lowQualityText;
		}
		
		public function qualityToMedium(e:Event = null):void
		{
			stage.quality = StageQuality.MEDIUM;
			
			_highQuality.caption = 		_highQualityText;
			_mediumQuality.caption = 	_mediumQualityText + " <<<";
			_lowQuality.caption = 		_lowQualityText;
		}
		
		public function qualityToLow(e:Event = null):void
		{
			stage.quality = StageQuality.LOW;
			
			_highQuality.caption = 		_highQualityText;
			_mediumQuality.caption = 	_mediumQualityText;
			_lowQuality.caption = 		_lowQualityText + " <<<";
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Right Click Menu Methods
		//
		//--------------------------------------------------------------------------
		
		protected function createRightClickMenu():void
		{
			_rightClickMenu = new ContextMenu();
			_rightClickMenu.hideBuiltInItems();
			
			createMenuItems();
			addMenuItems();
			
			contextMenu = _rightClickMenu;
			
			qualityToHigh();
		}
		
		protected function createMenuItems():void
		{
			_highQuality = new ContextMenuItem(_highQualityText);
			_highQuality.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, qualityToHigh);
			
			_mediumQuality = new ContextMenuItem(_mediumQualityText);
			_mediumQuality.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, qualityToMedium);
			
			_lowQuality = new ContextMenuItem(_lowQualityText);
			_lowQuality.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, qualityToLow);
			
			_siteLink = new ContextMenuItem("SuperFlashBros.net", true);
			_siteLink.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, browseToSFB);
		}
		
		protected function addMenuItems():void
		{
			if (_rightClickMenu.customItems)
			{
				_rightClickMenu.customItems.push(_highQuality);
				_rightClickMenu.customItems.push(_mediumQuality);
				_rightClickMenu.customItems.push(_lowQuality);
				
				_rightClickMenu.customItems.push(_siteLink);
			}
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Link Methods
		//
		//--------------------------------------------------------------------------
		
		public function browseToSFB(e:Event = null):void
		{
			navigateToURL(new URLRequest("http://www.superflashbros.net/"), "_blank");
		}
		
		public function browseToTV(e:Event = null):void
		{
			navigateToURL(new URLRequest("http://www.tomvian.com/"), "_blank");
		}
		
		public function browseToAG(e:Event = null):void
		{
			navigateToURL(new URLRequest("http://www.armorgames.com/"), "_blank");
		}
		
		public function browseToNG(e:Event = null):void
		{
			navigateToURL(new URLRequest("http://www.newgrounds.com/"), "_blank");
		}
		
		public function browseToURL(e:Event = null):void
		{
			navigateToURL(new URLRequest(_customURLs[e.target]), "_blank");
		}
		
		public function linkToSFB(button:InteractiveObject):void
		{
			button.addEventListener(MouseEvent.CLICK, browseToSFB);
		}
		
		public function linkToTV(button:InteractiveObject):void
		{
			button.addEventListener(MouseEvent.CLICK, browseToTV);
		}
		
		public function linkToAG(button:InteractiveObject):void
		{
			button.addEventListener(MouseEvent.CLICK, browseToAG);
		}
		
		public function linkToNG(button:InteractiveObject):void
		{
			button.addEventListener(MouseEvent.CLICK, browseToNG);
		}
		
		public function linkToURL(button:InteractiveObject, url:String):void
		{
			_customURLs[button] = url;
			button.addEventListener(MouseEvent.CLICK, browseToURL);
		}
		
		public function unlinkToSFB(button:InteractiveObject):void
		{
			button.removeEventListener(MouseEvent.CLICK, browseToSFB);
		}
		
		public function unlinkToTV(button:InteractiveObject):void
		{
			button.removeEventListener(MouseEvent.CLICK, browseToTV);
		}
		
		public function unlinkToAG(button:InteractiveObject):void
		{
			button.removeEventListener(MouseEvent.CLICK, browseToAG);
		}
		
		public function unlinkToNG(button:InteractiveObject):void
		{
			button.removeEventListener(MouseEvent.CLICK, browseToNG);
		}
		
		public function unlinkToURL(button:InteractiveObject):void
		{
			_customURLs[button] = null;
			button.removeEventListener(MouseEvent.CLICK, browseToURL);
		}
		
	}
}